﻿using log4net;
using System.Collections.Generic;
using System.ComponentModel;
using Telerik.Windows.Controls;
using TruckScale.Models;

namespace TruckScale.Views
{
  /// <summary>
  /// Interaction logic for DMUserDetailView.xaml
  /// </summary>
  public partial class DMUserDetailView : RadWindow, INotifyPropertyChanged
  {
    //log
    private static readonly log4net.ILog log = LogManager.GetLogger("DMUserDetailView");
    //新增后同步刷新列表
    public event PropertyChangedEventHandler PropertyChanged;

    public string InfoBar { get; set; }
    public string Theme { get; set; } = Global._weighConfig["Theme"];
    public string MainFontSize { get; set; } = Global._weighConfig["MainFontSize"];
    public UserModel User { get; set; }
    public List<RoleModel> RoleList { get; set; } = RoleModel.SelectActiveRole();
    public RoleModel SelectedRole { get; set; }
    public DMUserDetailView(UserModel user)
    {
      InitializeComponent();
      this.DataContext = this;

      if (user != null)
      {
        User = user;
        SelectedRole = RoleList.Find(r => r.RoleId == User.UserRole);
      }
      else
      {
        User = new UserModel();
        SelectedRole = RoleList[0];
      }
    }

    private void RadButton_Click(object sender, System.Windows.RoutedEventArgs e)
    {
      InfoBar = string.Empty;
      User.UserRole = SelectedRole.RoleId;
      if (User.UserId == null)
      {
        if (0 < UserModel.InsertUser(User))
        {
          this.Close();
        }
        else
        {
          InfoBar = "保存失败，请检查信息是否完整或已存在！";
        }
      }
      else
      {
        if (0 < UserModel.UpdateUser(User))
        {
          this.Close();
        }
        else
        {
          InfoBar = "保存失败，请检查信息是否完整或已存在！";
        }
      }
    }
  }
}
